const {config,changeConfig } = require("./config.js")
const { areKeysEqual } = require("./func.js")
const fs = require("fs")
const { ipcMain,dialog,app } = require("electron")

ipcMain.on("get-config", (event, arg) => {
    event.reply("get-config", config())
})

ipcMain.on("set-config", (event, arg) => {
    // 检查arg是否完整
    if (areKeysEqual(config(), arg)) {
        console.log("配置文件完整")
        // 写入配置文件
        changeConfig(arg)
    } else {
        dialog.showErrorBox("错误", "修改配置文件失败，请联系开发者")
        app.quit()
    }
})

ipcMain.on("set-config-item",(event,arg)=>{
    let key = arg.key
    let value = arg.value
    let config = config()
    let setting = config["setting"]
    // 判断key是否存在
    if (setting.hasOwnProperty(key)) {
        setting[key] = value
        config["setting"] = setting
        changeConfig(config)
    }else {
        dialog.showErrorBox("错误", "修改配置文件失败，请联系开发者")
        app.quit()
    }
})

ipcMain.on("get-config-item",(event,arg)=>{
    let key = arg
    let config = config()
    let setting = config["setting"]
    // 判断key是否存在
    if (setting.hasOwnProperty(key)) {
        event.reply("get-config-item",setting[key])
    }
})